package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.admin.api.entity.SysDept;
import com.engine4cloud.tdf.common.core.constant.SupervisionBusinessConstants;
import com.engine4cloud.tdf.common.core.util.CodeUtils;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.supervision.dto.create.GzrySocialWorkerCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.GzrySocialWorkerResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.GzrySocialWorkerUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.GzrySocialWorkerEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Mapper
public interface GzrySocialWorkerConvert {
    GzrySocialWorkerConvert INSTANCE = Mappers.getMapper(GzrySocialWorkerConvert.class);

    GzrySocialWorkerEntity createTo(GzrySocialWorkerCreateDTO s, Long tenantId);

    GzrySocialWorkerEntity updateTo(GzrySocialWorkerUpdateDTO s);

    GzrySocialWorkerResultDTO convertTo(GzrySocialWorkerEntity s);

    default GzrySocialWorkerEntity create(GzrySocialWorkerCreateDTO s, SysDept dept, Long tenantId) {
        GzrySocialWorkerEntity entity = createTo(s, tenantId);
        entity.setRybm(CodeUtils.entrustCode());
        entity.setDeptName(dept.getName());
        entity.setDeptId(dept.getDeptId());
        entity.setSsjg(dept.getOrgCode());
        additionalArea(entity, s.getAreaList());
        return entity;
    }

    default GzrySocialWorkerEntity update(GzrySocialWorkerUpdateDTO s) {
        GzrySocialWorkerEntity entity = updateTo(s);
        additionalArea(entity, s.getAreaList());
        return entity;
    }

    default GzrySocialWorkerResultDTO convert(GzrySocialWorkerEntity s) {
        GzrySocialWorkerResultDTO result = convertTo(s);
        renderAreaList(s, result);
        return result;
    }

    default void additionalArea(GzrySocialWorkerEntity s, List<String> areaList) {
        if (Objects.isNull(s) || CollectionUtils.isEmpty(areaList)) {
            return;
        }
        s.setJtzzszs(areaList.get(SupervisionBusinessConstants.AREA_PROVINCE_INDEX));
        s.setJtzzszds(areaList.get(SupervisionBusinessConstants.AREA_CITY_INDEX));

        if (areaList.size() >= SupervisionBusinessConstants.AREA_COUNTY_ARRAY_LENGTH) {
            s.setJtzzszxq(areaList.get(SupervisionBusinessConstants.AREA_COUNTY_INDEX));
        } else {
            s.setJtzzszxq(null);
        }

        if (areaList.size() >= SupervisionBusinessConstants.AREA_VILLAGE_ARRAY_LENGTH) {
            s.setJtzzszd(areaList.get(SupervisionBusinessConstants.AREA_VILLAGE_INDEX));
        } else {
            s.setJtzzszd(null);
        }
    }

    default void renderAreaList(GzrySocialWorkerEntity s, GzrySocialWorkerResultDTO t) {
        if (Objects.isNull(s) || Objects.isNull(t)) {
            return;
        }

        List<String> areaList = new ArrayList<>();
        Actions.apply(StringUtils.isNotEmpty(s.getJtzzszs()), () -> areaList.add(s.getJtzzszs()));
        Actions.apply(StringUtils.isNotEmpty(s.getJtzzszds()), () -> areaList.add(s.getJtzzszds()));
        Actions.apply(StringUtils.isNotEmpty(s.getJtzzszxq()), () -> areaList.add(s.getJtzzszxq()));
        Actions.apply(StringUtils.isNotEmpty(s.getJtzzszd()), () -> areaList.add(s.getJtzzszd()));

        t.setAreaList(areaList);
    }

}
